package com.guohong.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.guohong.common.RestResult;
import com.guohong.constant.ResponseCodeConstant;
import com.guohong.dto.RoleOptionDto;
import com.guohong.dto.RolePageDto;
import com.guohong.exception.ServiceException;
import com.guohong.service.RoleService;
import com.guohong.vo.RoleAddVo;
import com.guohong.vo.RoleUpdateVo;
import com.guohong.vo.RoleVo;

@RestController
@RequestMapping("/role")
public class RoleController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<RolePageDto> listRolesByPage(@RequestBody RoleVo roleVo) {
		RestResult<RolePageDto> result = new RestResult<>();

		try {
			RolePageDto rolePageDto = roleService.listRolesByPage(roleVo.getRoleName(), roleVo.getPageNum(),
					roleVo.getPageSize());

			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(rolePageDto);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	@RequestMapping(value = "/options", method = RequestMethod.GET)
	@ResponseBody
	public RestResult<List<RoleOptionDto>> getRoleOptions() {
		RestResult<List<RoleOptionDto>> result = new RestResult<>();

		try {
			List<RoleOptionDto> options = roleService.getRoleOptions();
			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(options);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<Integer> roleAdd(@RequestBody RoleAddVo roleAddVo) {
		RestResult<Integer> result = new RestResult<>();

		try {
			Integer count = roleService.addRole(roleAddVo);
			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(count);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public RestResult<Integer> roleUpdate(@RequestBody RoleUpdateVo roleUpdateVo) {
		RestResult<Integer> result = new RestResult<>();

		try {
			Integer count = roleService.updateRole(roleUpdateVo);
			result.setCode(ResponseCodeConstant.SUCCESS);
			result.setData(count);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setCode(e.getCode());
			result.setMsg(e.getMessage());

		} catch (Exception e) {
			e.printStackTrace();

			result.setCode(ResponseCodeConstant.FAILED);
			result.setMsg("系统内部错误");
		}

		return result;
	}
	
	
}
